package com.example.stars_project_android.personalCenter;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.stars_project_android.R;
import com.example.stars_project_android.index.CreateRoom;
import com.example.stars_project_android.index.Guestbook;
import com.example.stars_project_android.index.Index;
import com.example.stars_project_android.index.Messages;
import com.example.stars_project_android.index.Myguestbook;
import com.example.stars_project_android.index.MyRoom;
import com.example.stars_project_android.index.RoomDetail;
import com.example.stars_project_android.util.Constant;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 个人中心的页面
 */
public class User extends AppCompatActivity implements View.OnClickListener {

    private LinearLayout myHome;//我的房间
    private LinearLayout myStar;//我关注的星
    private LinearLayout mySay;//我的留言
    private LinearLayout mySetting;//设置
    private LinearLayout ceremony;//时空礼仪
    private TextView starId;    //星星id
    private TextView starDetil;   //星星信息描述
    private TextView textTime;  //登陆时间
//    private LinearLayout index, msg, add, talk, user;  //首页，留言板，创建时空，消息，我的

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user);

        //绑定控件对象
        setView();

        try {
            //设置数据
            setData();
        } catch (ParseException e) {
            e.printStackTrace();
        }

    }

    /**
     * 设置数据
     * @throws ParseException
     */
    private void setData() throws ParseException {
        starId.setText(Constant.starName);
        starDetil.setText(Constant.starName + "是一颗位于赤经" + Constant.longitude + "，赤纬 " + Constant.latitude +"的小星\n" +
                "                    星，距离地球" + Constant.distance + "光年");
        //获取当前时间
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// HH:mm:ss
        Date date = new Date(System.currentTimeMillis());
        //获取注册时间
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date endDate = df.parse(Constant.registerTime);
        //计算距离现在的天数
        int timeNum = getGapCount(date,endDate);
        textTime.setText("第" + timeNum +"天");
    }

    /**
     * 绑定控件对象
     */
    private void setView() {
        myHome = findViewById(R.id.my_home);
        myStar = findViewById(R.id.my_star);
        mySay = findViewById(R.id.my_say);
        mySetting = findViewById(R.id.my_setting);
        starId = findViewById(R.id.star_id);
        starDetil = findViewById(R.id.star_detil);
        textTime = findViewById(R.id.text_time);

        myHome.setOnClickListener(this);
        myStar.setOnClickListener(this);
        mySay.setOnClickListener(this);
        mySetting.setOnClickListener(this);
        ceremony.setOnClickListener(this);
//        index = findViewById(R.id.index);
//        index.setOnClickListener(this);
//        msg = findViewById(R.id.msg);
//        msg.setOnClickListener(this);
//        add = findViewById(R.id.add);
//        add.setOnClickListener(this);
//        talk = findViewById(R.id.talk);
//        talk.setOnClickListener(this);
//        user = findViewById(R.id.user);
//        user.setOnClickListener(this);
//        add = findViewById(R.id.add);
//        add.setOnClickListener(this);
    }

    /**
     * 计算登录的天数
     * @param startDate
     * @param endDate
     * @return
     */
    public int getGapCount(Date startDate, Date endDate) {
        Calendar fromCalendar = Calendar.getInstance();
        fromCalendar.setTime(startDate);
        fromCalendar.set(Calendar.HOUR_OF_DAY, 0);
        fromCalendar.set(Calendar.MINUTE, 0);
        fromCalendar.set(Calendar.SECOND, 0);
        fromCalendar.set(Calendar.MILLISECOND, 0);

        Calendar toCalendar = Calendar.getInstance();
        toCalendar.setTime(endDate);
        toCalendar.set(Calendar.HOUR_OF_DAY, 0);
        toCalendar.set(Calendar.MINUTE, 0);
        toCalendar.set(Calendar.SECOND, 0);
        toCalendar.set(Calendar.MILLISECOND, 0);

        return (int) ((toCalendar.getTime().getTime() - fromCalendar.getTime().getTime()) / (1000 * 60 * 60 * 24));
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.my_home:
                //跳转到我的房间页面
                Intent intent1 = new Intent();
                intent1.setClass(User.this, MyRoom.class);
                startActivity(intent1);
                break;
            case R.id.my_star:
                //跳转到我关注的星
                Intent intent2 = new Intent();
                intent2.setClass(User.this, Concernstar.class);
                startActivity(intent2);
                break;
            case R.id.my_say:
                //跳转到的留言
                Intent intent3 = new Intent();
                intent3.setClass(User.this, Myguestbook.class);
                startActivity(intent3);
                break;
            case R.id.my_setting:
                //跳转到设置
                Intent intent4 = new Intent();
                intent4.setClass(User.this, SetUp.class);
                startActivity(intent4);
                break;

            //底部导航栏点击跳转功能实现
//            case R.id.index:
//                //跳转到首页
//                Intent intent = new Intent(this, Index.class);
//                startActivity(intent);
//                break;
//            case R.id.msg:
//                //跳转到留言板页面
//                Intent intent5 = new Intent(this, Guestbook.class);
//                startActivity(intent5);
//                break;
//            case R.id.add:
//                //时空任意门
//                Intent intent6 = new Intent(this, RoomDetail.class);
//                startActivity(intent6);
//                break;
//            case R.id.talk:
//                //跳转到消息页面
//                Intent intent7 = new Intent(this, Messages.class);
//                startActivity(intent7);
//                break;
//            case R.id.user:
//                //跳转到我的页面
//                Intent intent8 = new Intent(this, User.class);
//                startActivity(intent8);
//                break;
            default:
                break;
        }

    }
}